﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class controls_registerControl : System.Web.UI.UserControl
{
    private ThanhVienDataAccess _thanhVienDa = new ThanhVienDataAccess();
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnDangKy_Click(object sender, EventArgs e)
    {
        try
        {
            
            string hoten = txtHoTen.Text.Trim();
            string diachi = txtDiaChi.Text.Trim();
            string email = txtEmail.Text.Trim();
            string sdt = txtSoDT.Text.Trim();
            string noilamviec = txtNoiLamViec.Text.Trim();
            string ngaysinh = Calendar1.SelectedDate.Date.ToString();
            string gioithieu = txtGioiThieu.Text.Trim();
            string hinh = fupHinh.FileName;
            string username = txtUsername.Text.Trim();
            string matkhau = txtPassword.Text.Trim();
            string nhaclaiMK = txtConfirmMK.Text.Trim();


            
            int result = _thanhVienDa.Register(new ThanhVien()
            {
                HoTen = hoten,
                DiaChi = diachi,
                SoDienThoai = sdt,
                NoiLamViec = noilamviec,
                NgaySinh = ngaysinh,
                GioiThieu = gioithieu,
                HinhAnh = hinh,
                Username = username,
                Password = matkhau, 
                Email = email
            });

            

            if (result > 0)
            {
                multiview1.ActiveViewIndex = 1;

                if (fupHinh.HasFile)
                {
                    try
                    {
                        if (fupHinh.PostedFile.ContentLength <= 200)
                        {
                            string filename = Path.GetFileName(fupHinh.FileName);
                            fupHinh.SaveAs(Server.MapPath("~/uploads/avatar/") + filename);

                            string url = "~/uploads/avatar/" + filename;
                            _thanhVienDa.updateAvatar(url, username);
                        }
                    }
                    catch (Exception ex) {                    
                    }
                }
            }
            else 
            {
                if(result == -2){
                    msg.Text = "Tài khoản hoặc email đã tồn tại!";
                }
            }
        }

        catch (Exception ex)
        {
            msg.Text = "Đăng ký thất bại!";
            throw ex;
        }

    }
 }
